package com.puhui.chome.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class WfProject {

    static String basePath = "C:\\system_code\\chome-monitor\\";
    /**
     * @param args
     */
    static String entityPackageName = "com.puhui.chome.model";
    static String srcPath = basePath + "src\\main\\java";
    static String resourcePath = basePath + "src\\main\\resources";
    static String sDao = srcPath + "\\com\\puhui\\chome\\dao\\"; // dao接口文件路径
    static String sManager = srcPath + "\\com\\puhui\\chome\\manager\\"; // manager实现类文件路径
    static String sService = srcPath + "\\com\\puhui\\chome\\service\\"; // service接口文件路径
    static String sServiceImpl = srcPath + "\\com\\puhui\\chome\\service\\impl\\"; // service接口实现类文件路径
    static String sController = srcPath + "\\com\\puhui\\chome\\controller\\";// controller文件路径

    static String srcTestPath = basePath + "src\\test\\java";
    static String sTestManager = srcTestPath + "\\com\\puhui\\chome\\manager\\"; // manager实现类文件路径
    static String sTestServiceImpl = srcTestPath + "\\com\\puhui\\chome\\service\\impl\\"; // service接口实现类文件路径
    static String sTestController = srcTestPath + "\\com\\puhui\\chome\\controller\\";// controller文件路径

    public static void main(String[] args) {
        faction("MonitorCc", "dingxingang");
    }

    public static void faction(String className, String author) {
        try {
            Class.forName(entityPackageName + "." + className);

            // dao
            writeDao(className, author);

            // manager
            writeManager(className, author);

            // Service接口
            writeService(className, author);

            // ServiceImpl
            writeServiceImpl(className, author);

            // controller
            writeController(className, author);

            // xml
            writeXml(className);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 写Dao
    private static void writeDao(String className, String author) {
        try {
            File fileAction = new File(sDao + className + "Dao.java");
            if (!fileAction.exists()) {
                String template = "";
                FileReader fr = new FileReader(sDao + "TemplateDao.java");

                BufferedReader br = new BufferedReader(fr);
                String s;
                while ((s = br.readLine()) != null) {
                    template += s + "\n";
                }
                fr.close();

                // 替换作者
                template = template.replaceAll("dingxingang", author);

                // 替换内容
                template = template.replaceAll("Template", className).replaceAll("template", toLower(className));

                FileWriter fw = new FileWriter(sDao + className + "Dao.java");
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(template);
                bw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 写Manager
    private static void writeManager(String className, String author) {
        try {
            File fileBiz = new File(sManager + className + "Manager.java");
            if (!fileBiz.exists()) {
                String template = "";
                FileReader fr = new FileReader(sManager + "TemplateManager.java");

                BufferedReader br = new BufferedReader(fr);
                String s;
                while ((s = br.readLine()) != null) {
                    template += s + "\n";
                }
                fr.close();

                // 替换作者
                template = template.replaceAll("dingxingang", author);

                // 替换内容
                template = template.replaceAll("Template", className).replaceAll("template", toLower(className));

                FileWriter fw = new FileWriter(sManager + className + "Manager.java");
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(template);
                bw.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 写Service
    private static void writeService(String className, String author) {
        try {
            File fileBiz = new File(sService + className + "Service.java");
            if (!fileBiz.exists()) {
                String template = "";
                FileReader fr = new FileReader(sService + "TemplateService.java");

                BufferedReader br = new BufferedReader(fr);
                String s;
                while ((s = br.readLine()) != null) {
                    template += s + "\n";
                }
                fr.close();

                // 替换作者
                template = template.replaceAll("dingxingang", author);

                // 替换内容
                template = template.replaceAll("Template", className).replaceAll("template", toLower(className));

                FileWriter fw = new FileWriter(sService + className + "Service.java");
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(template);
                bw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 写ServiceImpl
    private static void writeServiceImpl(String className, String author) {
        try {
            File fileBiz = new File(sServiceImpl + className + "ServiceImpl.java");
            if (!fileBiz.exists()) {
                String template = "";
                FileReader fr = new FileReader(sServiceImpl + "TemplateServiceImpl.java");

                BufferedReader br = new BufferedReader(fr);
                String s;
                while ((s = br.readLine()) != null) {
                    template += s + "\n";
                }
                fr.close();

                // 替换作者
                template = template.replaceAll("dingxingang", author);

                // 替换内容
                template = template.replaceAll("Template", className).replaceAll("template", toLower(className));

                FileWriter fw = new FileWriter(sServiceImpl + className + "ServiceImpl.java");
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(template);
                bw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 写Controller
    private static void writeController(String className, String author) {
        try {
            File fileBiz = new File(sController + className + "Controller.java");
            if (!fileBiz.exists()) {
                String template = "";
                FileReader fr = new FileReader(sController + "TemplateController.java");

                BufferedReader br = new BufferedReader(fr);
                String s;
                while ((s = br.readLine()) != null) {
                    template += s + "\n";
                }
                fr.close();

                // 替换作者
                template = template.replaceAll("dingxingang", author);

                // 替换内容
                template = template.replaceAll("Template", className).replaceAll("template", toLower(className));

                FileWriter fw = new FileWriter(sController + className + "Controller.java");
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(template);
                bw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 写XML
    private static void writeXml(String className) {
        try {
            File fileBiz = new File(resourcePath + "\\mybatis-mappers\\" + className + ".xml");
            if (!fileBiz.exists()) {
                String template = "";
                FileReader fr = new FileReader(resourcePath + "\\" + "Template.xml");

                BufferedReader br = new BufferedReader(fr);
                String s;
                while ((s = br.readLine()) != null) {
                    template += s + "\n";
                }
                fr.close();

                // 替换内容
                template = template.replaceAll("Template", className).replaceAll("template", toLower(className));

                // 获取实体属性，拼接数据库
                try {
                    Class clz = Class.forName(entityPackageName + "." + className);
                    String fieldXml = ""; // 属性映射
                    String fieldStr = ""; // 字段集合

                    // 类所有属性
                    List<String> fieldList = new ArrayList<String>();
                    // 父类获取
                    fieldList.addAll(getSupperXmlByClass(clz));
                    // 本类获取
                    fieldList.addAll(getXmlByClass(clz));
                    // 拼接映射XML
                    fieldXml = getXmlByFieldList(fieldList);
                    // 替换属性映射XML
                    template = template.replaceFirst("<attribute/>", fieldXml);
                    // 拼接数据库字段集合
                    fieldStr = getDbNameListByFieldList(fieldList);
                    template = template.replaceFirst("<dbNameList/>", fieldStr);
                    // 拼接实体字段集合
                    fieldStr = getFieldNameListByFieldList(fieldList);
                    template = template.replaceFirst("<fieldNameList/>", fieldStr);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

                FileWriter fw = new FileWriter(resourcePath + "\\mybatis-mappers\\" + className + ".xml");
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(template);
                bw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取父类的属性拼接xml
    private static List<String> getSupperXmlByClass(Class clz) {
        List<String> list = new ArrayList<String>();
        Class supClz = clz.getSuperclass();
        if (supClz != null) {
            list.addAll(getSupperXmlByClass(supClz));
            list.addAll(getXmlByClass(supClz));
        }
        return list;
    }

    // 获取类的属性拼接xml
    private static List<String> getXmlByClass(Class clz) {
        List<String> list = new ArrayList<String>();
        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields) {// --for() begin
            String name = field.getName(); // 字段名
            if (!StringUtil.isEmpty(name)) {
                list.add(name);
            }
        }
        return list;
    }

    // 获取类的属性拼接xml
    private static String getXmlByFieldList(List<String> fieldList) {
        String fieldXml = "";
        for (String name : fieldList) {// --for() begin
            if ("serialVersionUID".equals(name)) {
                continue;
            }
            fieldXml += "\t\t<result column=\"" + StringUtil.getFieldName(name) + "\" property=\"" + name + "\" />\n";
        }
        return fieldXml;
    }

    // 拼接数据字段集合
    private static String getDbNameListByFieldList(List<String> fieldList) {
        String fieldStr = "";
        for (String name : fieldList) {// --for() begin
            if ("serialVersionUID".equals(name) || "id".equals(name)) {
                continue;
            }
            fieldStr += "`" + StringUtil.getFieldName(name) + "`,";
        }
        if (fieldStr.length() > 0) {
            fieldStr = fieldStr.substring(0, fieldStr.length() - 1);
        }
        return fieldStr;
    }

    // 拼接实体字段集合
    private static String getFieldNameListByFieldList(List<String> fieldList) {
        String fieldStr = "";
        for (String name : fieldList) {// --for() begin
            if ("serialVersionUID".equals(name) || "id".equals(name)) {
                continue;
            }
            fieldStr += "\t\t\t#{" + name + "},\n";
        }
        if (fieldStr.length() > 0) {
            fieldStr = fieldStr.substring(0, fieldStr.length() - 2);
        }
        return fieldStr;
    }

    /**
     * 转小写（第一个字符转小写�?
     * 
     * @param str
     * @return
     */
    private static String toLower(String str) {
        String sNum = "";
        sNum = str.substring(0, 1).toLowerCase();
        sNum = sNum + str.substring(1);
        return sNum;
    }

}
